extends Node2D

var location: GenerateMapUtil.Location
@export var MOUSE_ENTERED_STYLEBOX: StyleBoxFlat
@export var MOUSE_EXITED_STYLEBOX: StyleBoxFlat
@export var MOUSE_ENTERED_PERFORMED_STYLEBOX: StyleBoxFlat

@onready var border_panel = $BorderPanel
var performed: bool = false


func init_room(icon, alocation):
	$BorderPanel/Icon.texture = icon
	$BorderPanel.anchors_preset = Control.PRESET_CENTER
	$BorderPanel.position = alocation.location
	self.location = alocation
	if location.level == 0:
		Globals.init_room = self


func _on_texture_rect_gui_input(event):
	if !is_from_neighbor_room():
		return
	if performed:
		return
	if event is InputEventMouseButton && event.button_index == MOUSE_BUTTON_LEFT:
		if event.is_pressed():
			location.res.perform(self)


func _on_icon_mouse_entered():
	if performed:
		border_panel.set("theme_override_styles/panel", MOUSE_ENTERED_PERFORMED_STYLEBOX)
	else:
		if !is_from_neighbor_room():
			return
		border_panel.set("theme_override_styles/panel", MOUSE_ENTERED_STYLEBOX)


func _on_icon_mouse_exited():
	border_panel.set("theme_override_styles/panel", MOUSE_EXITED_STYLEBOX)
		

func is_from_neighbor_room():
	var current_location:GenerateMapUtil.Location = Globals.current_room.location
	if current_location.neighbor.find(location) == -1:
		return false
	return true

func perform_down():
	performed = true
